package bit2;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.FutureTask;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

public class bitMain22 {
    public static void main(String[] args) {
        int sum=0;
        for (int i = 1; i < 101; i++) {
            if(i%7==0){
                sum+=i;
            }
        }
        System.out.println(sum);
    }
    public static void main5(String[] args) throws InterruptedException {
        CountDownLatch latch=new CountDownLatch(8);
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                System.out.println("疾跑");
                try {
                    Thread.sleep((long) (Math.random()* 10_000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                latch.countDown();
                System.out.println("撞线！");
            }
        };
        for (int i = 0; i < 8; i++) {
            Thread t=new Thread(runnable);
            t.start();
        }
        latch.await();
        System.out.println("比赛结束");
    }
    public static void main4(String[] args) {
        Semaphore semaphore=new Semaphore(4);
        Runnable runnable=new Runnable() {
            @Override
            public void run() {

                try {
                    System.out.println("准备申请资源");
                    semaphore.acquire();
                    System.out.println("申请资源成功");
                    Thread.sleep(1000);
                    System.out.println("释放资源完毕");
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        for (int i = 0; i < 20; i++) {
            Thread t=new Thread(runnable);
            t.start();
        }
    }

    static class Counter{
//        public int count=0;
//        public ReentrantLock locker=new ReentrantLock();
//        public void increase(){
//            locker.lock();
//            count++;
//            locker.unlock();
//        }
        public AtomicInteger count=new AtomicInteger(0);
        public void increase(){
            count.getAndIncrement();
        }
    }
    public static void main3(String[] args) {
        Counter  counter=new Counter();
        Thread t1=new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    counter.increase();
                }
            }
        };
        Thread t2=new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    counter.increase();
                }
            }
        };
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(counter.count);
    }
    public static void main2(String[] args) throws Exception {
        Callable<Integer> callable=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum=0;
                for (int i = 0; i <= 1000; i++) {
                    sum+=i;
                }
                return sum;
            }
        };
        FutureTask<Integer> futureTask=new FutureTask<>(callable);
        Thread t=new Thread(futureTask);
        t.start();
        Integer sum=futureTask.get();
        System.out.println(sum);
    }
    static class Result{
        public int sum=0;
        public Object locker=new Object();
    }
    public static void main1(String[] args) throws InterruptedException {
        Result result=new Result();
        Thread t=new Thread(){
            @Override
            public void run() {
                int sum=0;
                for (int i = 0; i <= 1000; i++) {
                    sum+=i;
                }
                result.sum=sum;
                synchronized (result.locker){
                    result.locker.notify();
                }
            }
        };
        t.start();
        synchronized (result.locker) {
            while (result.sum == 0) {
                result.locker.wait();
            }
        }
        System.out.println(result.sum);
    }
}
